package com.zhiyou.BufferedInputStream;

import java.io.*;
import java.util.ArrayList;

public class BufferedInputStreamTest {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Person p1 = new Person("刘备", 27, "蜀国");
        Person p2 = new Person("关羽", 25, "蜀国");
        Person p3 = new Person("张飞", 24, "蜀国");

        ArrayList<Person> list = new ArrayList<>();
        list.add(p1);
        list.add(p2);
        list.add(p3);
        BufferedInputStreamTest bs = new BufferedInputStreamTest();


        bs.readObj(list);
        bs.inputObj(list);
    }
    public void readObj(ArrayList<Person> list) throws IOException {
        File file = new File("D:\\xxx.txt");
        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        oos.writeObject(list);

        oos.close();
        fos.close();
    }

    public void inputObj(ArrayList<Person> list) throws IOException, ClassNotFoundException {
        File file = new File("D:\\xxx.txt");
        FileInputStream in = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(in);

        ois.readObject();
        for (Person p: list) {
            System.out.println(p.getName()+""+p.getAddress());
        }
        ois.close();
        in.close();
    }


}
